import Flutter
import UIKit

@main
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    
    // 获取 Flutter 引擎
    let controller: FlutterViewController = window?.rootViewController as! FlutterViewController
    let channel = FlutterMethodChannel(name: "my.test.channel", binaryMessenger: controller.binaryMessenger)
    
    // 设置方法回调
    channel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
      switch call.method {
      case "getMessage":
        result("来自 iOS 原生的问候 👋")
      case "printLog":
        if let args = call.arguments as? [String: Any],
           let msg = args["msg"] as? String {
          print("Flutter 发来的消息: \(msg)")
        }
        result(nil)
      default:
        result(FlutterMethodNotImplemented)
      }
    }
    
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}
